package com.hpdz.hotel_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hpdz.hotel_back.entity.Admin;
import com.hpdz.hotel_back.entity.Customer;
import com.hpdz.hotel_back.entity.Employee;
import com.hpdz.hotel_back.entity.Human;
import com.hpdz.hotel_back.mapper.AdminMapper;
import com.hpdz.hotel_back.mapper.CustomerMapper;
import com.hpdz.hotel_back.mapper.EmployeeMapper;
import com.hpdz.hotel_back.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Transactional
@Slf4j
@Service
public class CommonServiceImpl implements CommonService {
    @Resource
    private AdminMapper adminMapper;

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public Human findTheMan(String email) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq(true, "email", email);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);

        QueryWrapper<Employee> employeeQueryWrapper= new QueryWrapper<>();
        employeeQueryWrapper.eq(true,"email",email);
        Employee employee = employeeMapper.selectOne(employeeQueryWrapper);


        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq(true,"email",email);
        Customer customer = customerMapper.selectOne(customerQueryWrapper);

        if(admin != null) return admin;
        if(employee != null) return employee;
        if(customer != null) return customer;
        return null;
    }

    @Override
    public Boolean updatePass(Human theMan, String password) {
        if(theMan instanceof Admin){
            Admin admin = (Admin)(theMan);
            admin.setPassword(password);
            return adminMapper.updateById(admin) == 1;
        }else if(theMan instanceof Customer){
            Customer customer = (Customer)(theMan);
            customer.setPassword(password);
            return customerMapper.updateById(customer) == 1;
        }else {
            Employee employee = (Employee)(theMan);
            employee.setPassword(password);
            return employeeMapper.updateById(employee) == 1;
        }
    }

}
